<template>
  <div>
    <el-table
      v-loading="loading"
      ref="table"
      :data="tableData"
      style="width: 100%"
      :height="height"
      fit
      row-key="id"
      :default-sort="{ prop: sortProp, order: 'descending' }"
    >
      <el-table-column type="index" align="center" label="序号" width="80">
      </el-table-column>
      <template v-for="(item, index) in tabtitle">
        <el-table-column
          v-if="!item.render"
          :key="index"
          show-overflow-tooltip
          :prop="item.prop"
          :label="item.label"
          :width="item.width || ''"
          :sortable="item.sortable"
          :align="item.align || 'center'"
        >
        </el-table-column>
        <el-table-column
          v-if="!!item.render"
          :key="index"
          show-overflow-tooltip
          :label="item.label"
          :width="item.width || ''"
          :sortable="item.sortable"
          :align="item.align || 'center'"
        >
          <template slot-scope="scope">
            <expandDom :render="item.render" :scope="scope" />
          </template>
        </el-table-column>
      </template>

      <slot></slot>
    </el-table>

    <el-pagination
      v-if="showPagination"
      ref="pagination"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  components: {
    expandDom: {
      functional: true,
      props: {
        render: Function,
        scope: Object
      },
      render: (h, ctx) => {
        return ctx.props.render(h, ctx.props.scope)
      }
    }
  },
  props: {
    tableData: {
      type: Array,
      default: []
    },
    tabtitle: {
      type: Array,
      default: []
    },
    height: {
      type: String,
      default: ''
    },
    loading: {
      type: Boolean,
      default: true
    },
    showPagination: {
      type: Boolean,
      default: true
    },
    current: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 10
    },
    total: {
      type: Number,
      default: 0
    },
    sortProp: {
      type: String,
      default: 'createTime'
    },
    pageSizes: {
      type: Array,
      default: () => [10, 20, 30, 50]
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('handleSizeChange', val)
    },
    handleCurrentChange(val) {
      this.$emit('handleCurrentChange', val)
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .el-pagination {
  position: relative;
  .el-pagination__total {
    position: absolute;
    left: 0;
  }
}
</style>
